<?php
// +----------------------------------------------------------------------
// | 车钉钉 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN
// +----------------------------------------------------------------------

namespace app\admin\controller;

/**
 * 品牌控制器
 */
class Brand extends Base{
    /**
     * 前置操作方法列表
     * @var array $beforeActionList
     * @access protected
     */
    protected $beforeActionList = ['baseInfo'];
    /**
     * 构造方法，初始化站点信息
     */
    protected function _initialize(){
        parent::_initialize();
    }
    //模版信息初始化
    public function baseInfo(){
        $baseWebmsg=cache('baseWebMsg');
        $this->assign('baseWebMsg',$baseWebmsg);
    }
    
	//品牌列表
    public function index(){
        $brandList=db('car_brand')->where('pid',0)->select();
        $this->assign('brandList',$brandList);
        //dump($brandList);
        //exit();
        return $this->fetch();
    }
    //添加品牌页面
    public function brandAdd(){
        $id=input('param.id');//品牌id
        if(isset($id)){
            $brandone=db('car_brand')->where('id',$id)->find();
            $this->assign('brandone',$brandone);
        }
        return $this->fetch();
    }
    //ajax新增、更新操作 
    public function brandSave(){
        $id=input('post.id/d');//有id则是更新操作
        $name=input('post.name');
        $rank=input('post.rank/d');
        $base64=input('post.img');
        $city=input('post.city');
        $msg=input('post.msgtext','','strip_tags,htmlspecialchars');
        $isDisplay=input('post.isdisplay/d');
        //$uid=session('uid');
        if(empty($name)||empty($rank)||empty($base64)){
            return ['code'=>300,'msg'=>'*为必填项'];
        }elseif (!empty($id)){
            $isName=db('car_brand')->where('id',$id)->find();
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
                $type = $result[2];//后缀
                $fileName=time();
                $new_file ="static/img/brand/".$fileName.".".$type;//图片保存路径
                $m_new_file ="static/img/brand/M_".$fileName.".".$type;//图片保存路径
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){//保存原图
                    $image = \think\Image::open($new_file);
                    $image->thumb(180,120,\think\Image::THUMB_SCALING)->save($m_new_file);//生成缩略图
                    $data=['name'=>$name,'logo'=>$fileName.".".$type,'isdisplay'=>$isDisplay,'rank'=>$rank,'city'=>$city,'text'=>$msg,'create_time'=>time()];
                    @unlink("./static/img/brand/".$isName['logo']);
                    @unlink("./static/img/brand/M_".$isName['logo']);
                    $isOK=db('car_brand')->where('id',$id)->update($data);
                    if($isOK){
                        return ['code'=>200,'msg'=>'修改成功'];
                    }else{
                        return ['code'=>400,'msg'=>'修改失败'];
                    }
                }else{
                    return ['code'=>300,'msg'=>'保存图片失败'];
                }
            }else {
                return ['code'=>300,'msg'=>'图片格式不正确'];
            }
        }
        $isName=db('car_brand')->where('name',$name)->find();
        if(!empty($isName)){
            return ['code'=>300,'msg'=>'品牌已存在'];
        }
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
            $type = $result[2];//后缀
            $fileName=time();
            $new_file ="static/img/brand/".$fileName.".".$type;//图片保存路径
            $m_new_file ="static/img/brand/M_".$fileName.".".$type;//图片保存路径
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){//保存原图
                $image = \think\Image::open($new_file);
                $image->thumb(180,120,\think\Image::THUMB_SCALING)->save($m_new_file);//生成缩略图 
                $data=['name'=>$name,'logo'=>$fileName.".".$type,'isdisplay'=>$isDisplay,'rank'=>$rank,'city'=>$city,'text'=>$msg,'create_time'=>time()];
                $isOK=db('car_brand')->insert($data);
                if($isOK){
                    return ['code'=>200,'msg'=>'提交成功'];
                }else{
                    return ['code'=>400,'msg'=>'提交失败'];
                }
            }else{
                return ['code'=>300,'msg'=>'保存图片失败'];
            }
        }else {
            return ['code'=>300,'msg'=>'图片格式不正确'];
        }
    }
    
    //ajax删除
    public function delete(){
        $id=input('param.id/d');//品牌id
        $url=db('car_brand')->where('id',$id)->find();
        $isOK=db('car_brand')->delete($id);
        if($isOK){
            @unlink('./static/img/brand/'.$url['logo']);
            @unlink('./static/img/brand/M_'.$url['logo']);
            return ['code'=>200,'msg'=>'删除成功'];
        }else{
            return ['code'=>400,'msg'=>'删除失败'];
        }
    }
    //ajax修改显示
    public function isDisplay(){
        $id=input('param.id/d');//品牌id
        $isDisplay=db('car_brand')->where('id',$id)->find();
        if($isDisplay['isdisplay']==1){
            $isOK=db('car_brand')->where('id',$id)->update(['isdisplay'=>0]);
        }else{
            $isOK=db('car_brand')->where('id',$id)->update(['isdisplay'=>1]);
        }
        if($isOK){
            return ['code'=>200,'msg'=>'修改成功'];
        }else{
            return ['code'=>400,'msg'=>'修改失败'];
        }
    }
}
